﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.IO;
using System.Xml;
using System.Windows;
using System.Threading;
using System.Windows.Forms;
using System.Xaml;

namespace X3DXamlXna
{
    public interface ICore
    { }

    public class Core : GameComponent, ICore
    {
        X3D myX3d;
        CameraManager Cameras;
        BasicEffect effect;
        string CameraName;
        public string FileName { get; set; }
        public string FilePath { get; set; }


        public Core(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(ICore), this);
            Cameras = new CameraManager(Game);
            Game.Components.Add(Cameras);
            Game.Components.Add(new DrawableComponent(Game));
            Game.Components.Add(new SpaceSubdivisionComponent(Game));
            Game.Components.Add(new TouchComponent(Game));
            Game.Components.Add(new ProximityComponent(Game));
        }

        public override void Initialize()
        {
            /// and initialize them as well.
            /// 

            Game.Window.Title = "Loading " + FileName;

            System.IO.Directory.CreateDirectory("parts");

            StreamReader reader = new StreamReader(FileName);
            string content = reader.ReadToEnd();
            reader.Close();

            String searchText = "<X3D profile='Immersive' >";
            String replaceText = "<X3D xmlns=\"clr-namespace:X3DXamlXna;assembly=X3DXamlXna\" profile='Immersive'>";
            content = System.Text.RegularExpressions.Regex.Replace(content, searchText, replaceText);

            byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(content);
            MemoryStream stream = new MemoryStream(byteArray);

            var xml = new XmlTextReader(stream);

            //var xaml = new XamlXmlReaderSettings();
            //xaml.LocalAssembly = System.Reflection.Assembly.GetAssembly(typeof(Scene));
            //var polok = new XamlXmlReader(stream, xaml);



            myX3d = XamlServices.Load(xml) as X3D;            

            Mouse.SetPosition(Game.GraphicsDevice.Viewport.Width / 2, Game.GraphicsDevice.Viewport.Height / 2);
            base.Initialize();

        }

        public void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.          
            effect = new BasicEffect(Game.GraphicsDevice);


           // Game.GraphicsDevice.RasterizerState.CullMode = CullMode.None;

            Game.GraphicsDevice.BlendState = BlendState.Opaque;
            Game.GraphicsDevice.RasterizerState = RasterizerState.CullNone;
            Game.GraphicsDevice.DepthStencilState = DepthStencilState.Default;


            effect.LightingEnabled = true;
            effect.PreferPerPixelLighting = true;
            effect.EnableDefaultLighting();

            myX3d.initialize(Game);
            (Game.Services.GetService(typeof(ISpaceSubdivisionComponent)) as ISpaceSubdivisionComponent).start();


            // TODO: use this.Content to load your game content here
        }

        public override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (!Cameras.CurrentCamera.DEF.Equals(CameraName))
            {
                CameraName = Cameras.CurrentCamera.DEF;
                Game.Window.Title = FileName.Split('\\').Last() + " @ " + CameraName;
            }

            base.Update(gameTime);
        }

    }
}
